package com.tenebrous.jestic.controller.framwork.system.notice;

import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.system.SysNotice;
import com.tenebrous.jestic.entity.framwork.system.SysNoticeRead;
import com.tenebrous.jestic.service.framwork.system.SysNoticeService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 通知公告
 */
@Validated
@RestController
@RequestMapping("/notice")
public class NoticeController extends SupperController {

    @Autowired
    private SysNoticeService noticeService;

    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        SysNotice item = noticeService.selectByPrimaryKey(id);
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 预览公告
     *
     * @return
     */
    @PostMapping("/preview")
    public ApiResult preview(@NotBlank String id) {
        SysNotice item = noticeService.selectByPrimaryKey(id);
        if (item != null) {
            item.setTotalReadedCount(noticeService.getReadedCount(id));
        }
        return new ApiResult(200, "查询成功。", item);
    }

    /**
     * 浏览公告
     *
     * @return
     */
    @PostMapping("/view")
    public ApiResult view(@NotBlank String id) {
        OrgUser ouser = super.getCurrentUser();
        SysNotice item = noticeService.selectByPrimaryKey(id);
        if (item != null) {
            if (Integer.valueOf(1).equals(item.getPublished())) {
                SysNoticeRead readittem = new SysNoticeRead();
                readittem.setId(UUIDUtils.randomUUID());
                readittem.setNoticeid(item.getId());
                readittem.setCreateUserid(ouser != null ? ouser.getId() : null);
                readittem.setCreateDate(new Date());
                noticeService.saveReadSelective(readittem);
                item.setTotalReadedCount(noticeService.getReadedCount(id));

                return new ApiResult(200, "查询成功。", item);
            } else {
                return new ApiResult(999, "查看失败，未发布的公告。");
            }
        }
        return new ApiResult(999, "查看失败，未找到指定对象。");
    }

    /**
     * 查询
     *
     * @return
     */
    @PostMapping("/paging")
    public ApiResult paging(String stype, String title) {
        PageInfo pageInfo = noticeService.queryPagingList(super.getPageIndex(), super.getPageSize(), stype, title);
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    @PostMapping("/paging/published")
    public ApiResult pagingPublished() {
        OrgUser ouser = super.getCurrentUser();
        PageInfo<SysNotice> pageInfo = noticeService.queryPagingList(super.getPageIndex(), super.getPageSize(),null, 1);
        for (SysNotice item : pageInfo.getList()) {
            Integer cnt = noticeService.getUserReadedCount(item.getId(), ouser.getId());
            item.setUserReadedCount(cnt);
        }
        return new ApiResult(200, "查询成功。", pageInfo);
    }

    /**
     * 获取当前用户未读通知数量
     *
     * @return
     */
    @PostMapping("/unread/count")
    public ApiResult unreadCount() {
        OrgUser ouser = super.getCurrentUser();
        return new ApiResult(200, "查询成功。", noticeService.unreadCount(ouser.getId()));
    }

    /**
     * 新增或更新对象
     *
     * @param item
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新系统通知公告", method = WeblogEnum.UPDATE)
    public ApiResult update(@NotNull @Validated SysNotice item) {
        if (StringUtils.isNotBlank(item.getId())) {
            SysNotice item1 = noticeService.selectByPrimaryKey(item.getId());
            if (item1.getPublished() == 1) {
                return new ApiResult(999, "修改失败，此条信息已发布。");
            }
            OrgUser ouser = super.getCurrentUser();
            item.setUpdateDate(new Date());
            item.setUpdateUserid(ouser.getId());
            noticeService.updateSelective(item);
        } else {
            OrgUser ouser = super.getCurrentUser();
            item.setId(UUIDUtils.randomUUID());
            item.setPublished(0);
            item.setCreateUserid(ouser.getId());
            item.setCreateDate(new Date());
            noticeService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }

    /**
     * 批量删除对象
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除指定系统通知公告", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotEmpty String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            SysNotice item = noticeService.selectByPrimaryKey(ids[i]);
            if (item.getPublished() == 1) {
                return new ApiResult(999, "删除失败，包含了已发布信息。");
            }
        }
        //List item = noticeService.queryByIds(ids);
        return new ApiResult(200, "删除成功。", noticeService.deleteByIds(ids));
    }

    /**
     * feign发送系統消息
     *
     * @param item
     * @return
     */
    @PostMapping("/send")
    @Weblog(description = "发送系统通知公告", method = WeblogEnum.UPDATE)
    public ApiResult sendSys(@NotNull SysNotice item) {
        OrgUser ouser = super.getCurrentUser();
        item.setId(UUIDUtils.randomUUID());
        item.setPublished(1);
        item.setCreateDate(new Date());
        item.setCreateUserid(ouser != null ? ouser.getId() : null);
        noticeService.saveSelective(item);
        return new ApiResult(200, "更新成功。");
    }
}
